package com.boc.langxiang.presenter.shop;

import com.boc.langxiang.contract.shop.ShopIndexContract;
import com.boc.langxiang.entity.bean.ShopBannerInfo;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.ShopGoodsContent;
import com.boc.langxiang.entity.response.ShoppingCarNumContent;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.HandleErrorSubscriber;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/7/5.
 */
public class ShopIndexPresenter extends SuperPresenter implements ShopIndexContract.Presenter, ShopIndexContract {

    private View mView;

    public ShopIndexPresenter(View view) {
        super(view);
        this.mView = view;
    }

    @Override
    public void getBanner(final CallBack callBack) {
        mService.shopBanner(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<List<ShopBannerInfo>>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<List<ShopBannerInfo>> response) {
                        callBack.onCallBack(response);
                    }
                });

    }

    @Override
    public void getGoods(final CallBack callBack) {
        mService.shopGoods(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShopGoodsContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShopGoodsContent> response) {
                        callBack.onCallBack(response);
                    }
                });
    }

    @Override
    public void getAllProducts(final CallBack callBack) {
        mService.shopAllProduct(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShopGoodsContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShopGoodsContent> response) {
                        callBack.onCallBack(response);
                    }
                });
    }

    @Override
    public void getSpecialTopic(final CallBack callBack) {
        mService.shopSpecialTopic(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShopGoodsContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShopGoodsContent> response) {
                        callBack.onCallBack(response);
                    }
                });
    }

    @Override
    public void getShoppingCarNumber() {
        mService.getShoppingCarNum(UserInfoManager.getInstance().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShoppingCarNumContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShoppingCarNumContent> response) {
                        mView.onGetShoppingCarNumberSuccess(response);
                    }
                });
    }

    @Override
    public void star() {
        mView.showLoading();
        getGoods(new CallBack() {
            @Override
            public void onCallBack(BaseResponse response) {
                mView.onGetGoodsSuccess(response);
                getSpecialTopic(new CallBack() {
                    @Override
                    public void onCallBack(BaseResponse response) {
                        mView.onGetSpecialTopicSuccess(response);
                        getAllProducts(new CallBack() {
                            @Override
                            public void onCallBack(BaseResponse response) {
                                mView.onGetAllProductSuccess(response);
                                getBanner(new CallBack() {
                                    @Override
                                    public void onCallBack(BaseResponse response) {
                                        mView.hideLoading();
                                        mView.onGetBannerSuccess(response);
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    }


}
